<?php
require_once("projectView.php");
require_once(CORE_DIR.'projectModel.php');
require_once(CORE_DIR.'userModel.php');
require_once(CORE_DIR.'permissionModel.php');

class getAction extends authAction{

	function __construct(){
		$this->view = new projectView();
	}

	public function run($get, $post, $viewtype) {
		parent::run($get,$post,$viewtype);
		
		$pModel = new projectModel();	
		
		if(isset($get["userid"])) {
			$projects = $pModel->getUserProjects($get["userid"]);
		}
		else if(isset($get["archive"])) {
			$projects = $pModel->getArchivedProjects();
		}
		else {
			$uModel = new userModel();
			$activeUser = $uModel->getActiveUser();
			
			if ($activeUser->getRole() == 2) {
				$projects = $pModel->getPermittedProjects($activeUser->getUserId());
			}
			else {
				$permModel = new permissionModel();
				$permissions = $permModel->getAllPermissions();
				$projects = $pModel->getProjects();

				$index = 0;
				foreach ($projects as $project)
				{
					$users = array();
					foreach ($permissions as $permission)
					{
						if ($project->getId() == $permission->getProjectId())
						{
							$users[] = $permission->getUserId()."|".$permission->getPermission();
						}
					}
					$users = implode(";", $users);
					
					$userList[$project->getId()] = $users;
					$index++;
				}
			}
		}
		$this->view->setProperty("projects", $projects);
		$this->view->setProperty("users", $userList);
		$this->view->showGetProject();
		
	}
}
?>